from flask import Flask
from flask_admin import Admin
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from config import config

db = SQLAlchemy()
lm = LoginManager()
lm.login_view = 'blog.login'
from . import models
from .models import User, Post, Comment
from .admin import MyView

# Flask and Flask-SQLAlchemy initialization here

def create_app(config_name):
    app = Flask(__name__)
    db.init_app(app)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    lm.init_app(app)

    from .blog import blog as blog_blueprint
    app.register_blueprint(blog_blueprint)

    admin = Admin(app)
    admin.add_view(MyView(User, db.session))
    admin.add_view(MyView(Post, db.session))
    admin.add_view(MyView(Comment, db.session))

    return app



